Skip to content

Conversation

@jlc-christie
Copy link

Description

As described in #3002, the current default behaviour results in all celery tasks that execute are child spans of the code that pushed it on to the broker, which conflicts with the semantic conventions.

This PR doesn't change the default behaviour since this may be undesirable for consumers expecting the behaviour to be consistent.

Fixes #3002 by providing an optional parameter to the .instrument() method to allow for optionally using span links, e.g.

CeleryInstrumentor().instrument(use_links=True)

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Unit test added
  • Tested E2E locally with jaeger with use_links=False and also use_links=True and observed the expected behaviour: when the flag is False (default) all task executions of a fan-out job are child spans of the code that enqueued it and when True they appeared as linked spans.

Does This PR Require a Core Repo Change?

  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@jlc-christie jlc-christie requested a review from a team as a code owner September 24, 2025 16:24
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Sep 24, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

Comment on lines +266 to +268
SpanAttributes.MESSAGING_DESTINATION: "celery",
},
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section of code is just duplicated from test_task, the remaining assertions below are the only meaningful changes

@jlc-christie jlc-christie force-pushed the celery-allow-using-links-instead-of-child-spans branch from b87b7a0 to 541face Compare September 26, 2025 08:00
@jlc-christie jlc-christie force-pushed the celery-allow-using-links-instead-of-child-spans branch from 541face to b87b7a0 Compare September 26, 2025 11:40
@xrmx xrmx moved this to Ready for review in @xrmx's Python PR digest Sep 30, 2025
@jlc-christie jlc-christie force-pushed the celery-allow-using-links-instead-of-child-spans branch from 99cc8be to 4f1eda6 Compare October 7, 2025 12:59
@jlc-christie
Copy link
Author

@xrmx just rebased latest from main onto this branch, lmk if it's easier for your review process to just let it drift and rebase after you've reviewed

The ``CeleryInstrumentor().instrument()`` method accepts the following arguments:
* ``use_links`` (bool): When ``True``, Celery task execution spans will be linked to the
Copy link
Contributor

@xrmx xrmx Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From #3002 (comment) I understand we should always add links but make the parent / child relationship optional instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a "one-or-the-other" situation to me...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a "one-or-the-other" situation to me...

Agreed, I went with a default of False since it seemed like this would be a pretty major change for consumers of this package that weren't expecting the update without warning, whilst still giving consumers who care about this an option to opt-out of the current behaviour.

Maybe the changing of the default from False to True could be packaged up in a future release and a warning logged mentioning the change in that future release so people are aware of the incoming change?

Personally no objection from my side for changing the default though so if this isn't as contentious as I'm imagining it would be then I'm happy to flip the value.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xrmx are you happy with this approach? As mentioned I don't feel strongly either way but don't want to leave this PR open too long and end up drifting from main.

tracer_provider,
schema_url="https://opentelemetry.io/schemas/1.11.0",
)
# pylint: disable=attribute-defined-outside-init
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this PR, but this pylint rule seems useless in this project, can we disable it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True... I added it to keep it consistent with the rest of the file but I'll add an issue to the project for this

@jlc-christie jlc-christie force-pushed the celery-allow-using-links-instead-of-child-spans branch from d6f5fa1 to 3bd9d2e Compare October 10, 2025 14:56
@jlc-christie jlc-christie requested a review from xrmx October 10, 2025 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Ready for review

Development

Successfully merging this pull request may close these issues.

Should celery add a span link instead?

4 participants